'
' DotNetNuke - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports System.IO
Imports System.Runtime.Serialization.Formatters
Imports DotNetNuke

Namespace DotNetNuke.Modules.UsersOnline

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The Settings ModuleSettingsBase is used to manage the 
    ''' settings for the Users Online Module
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    '''		[cnurse]	10/25/2004	Converted to DNN v3.0
    ''' 	[dbuckner]	06/05/2009	Converted to ASP.Net 2x &amp; DNN v4.9.4
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class Settings

        Inherits Entities.Modules.ModuleSettingsBase

#Region "Controls"
        Protected imgStatus As System.Web.UI.WebControls.Image
        Protected lblStatus As System.Web.UI.WebControls.Label
        Protected ddlNameDisplayMode As System.Web.UI.WebControls.DropDownList
        Protected ddlNamePrefix As System.Web.UI.WebControls.DropDownList
        Protected chkIncludeHosts As System.Web.UI.WebControls.CheckBox
        Protected chkShowMembership As System.Web.UI.WebControls.CheckBox
        Protected chkDisplayLatestMember As System.Web.UI.WebControls.CheckBox
        Protected chkDisplayPast24Hours As System.Web.UI.WebControls.CheckBox
        Protected chkDisplayPrev24Hours As System.Web.UI.WebControls.CheckBox
        Protected chkDisplayOverallCount As System.Web.UI.WebControls.CheckBox
        Protected chkShowPeopleOnline As System.Web.UI.WebControls.CheckBox
        Protected chkDisplayVisitors As System.Web.UI.WebControls.CheckBox
        Protected chkDisplayMembers As System.Web.UI.WebControls.CheckBox
        Protected chkDisplayOnlineTotals As System.Web.UI.WebControls.CheckBox
        Protected chkShowUsersOnline As System.Web.UI.WebControls.CheckBox
        'Protected WithEvents rblstProfileMode As System.Web.UI.WebControls.RadioButtonList
        'Protected trUserProfileUrl As System.Web.UI.HtmlControls.HtmlTableRow
        'Protected txtUserProfileUrl As System.Web.UI.WebControls.TextBox
#End Region

#Region "Base Method Implementations"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' LoadSettings loads the settings from the Databas and displays them
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''		[cnurse]	10/25/2004	created
        '''  	[dbuckner]	06/05/2009	updated
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Overrides Sub LoadSettings()
            Try
                If (Page.IsPostBack = False) Then
                    'Populate Display Names
                    ddlNameDisplayMode.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("DisplayName", Me.LocalResourceFile), "DisplayName"))
                    ddlNameDisplayMode.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("UserName", Me.LocalResourceFile), "UserName"))
                    ddlNameDisplayMode.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("FirstName", Me.LocalResourceFile), "FirstName"))
                    ddlNameDisplayMode.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("LastName", Me.LocalResourceFile), "LastName"))
                    ddlNameDisplayMode.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("FullName", Me.LocalResourceFile), "FullName"))

                    'Populate Name Prefixs
                    ddlNamePrefix.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("NamePrefix_None", Me.LocalResourceFile), "None"))
                    ddlNamePrefix.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("NamePrefix_Image", Me.LocalResourceFile), "Image"))
                    ddlNamePrefix.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("NamePrefix_Numbers", Me.LocalResourceFile), "Numbers"))

                    'Populate UserProfileTypes
                    'rblstProfileMode.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("UserProfileMode_Off", Me.LocalResourceFile), "0"))
                    'rblstProfileMode.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("UserProfileMode_On", Me.LocalResourceFile), "1"))
                    'rblstProfileMode.Items.Add(New System.Web.UI.WebControls.ListItem(Localization.GetString("UserProfileMode_Url", Me.LocalResourceFile), "2"))

                    If (ModuleSettings("ShowMembership") Is Nothing) Then
                        chkIncludeHosts.Checked = False
                        chkShowMembership.Checked = True
                        chkDisplayLatestMember.Checked = True
                        chkDisplayPast24Hours.Checked = True
                        chkDisplayPrev24Hours.Checked = True
                        chkDisplayOverallCount.Checked = True
                        chkShowPeopleOnline.Checked = True
                        chkDisplayVisitors.Checked = True
                        chkDisplayMembers.Checked = True
                        chkDisplayOnlineTotals.Checked = True
                        chkShowUsersOnline.Checked = True
                        ddlNameDisplayMode.Items.FindByValue("DisplayName").Selected = True
                        ddlNamePrefix.Items.FindByValue("Numbers").Selected = True 'Preserve Original behavior
                        'rblstProfileMode.SelectedIndex = 0
                        'txtUserProfileUrl.Text = String.Empty
                    Else
                        chkShowMembership.Checked = CType(ModuleSettings("ShowMembership"), Boolean)
                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("DisplayLatestMember") Is Nothing Then
                            chkDisplayLatestMember.Checked = True
                        Else
                            chkDisplayLatestMember.Checked = CType(ModuleSettings("DisplayLatestMember"), Boolean)
                        End If

                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("DisplayPast24Hours") Is Nothing Then
                            chkDisplayPast24Hours.Checked = True
                        Else
                            chkDisplayPast24Hours.Checked = CType(ModuleSettings("DisplayPast24Hours"), Boolean)
                        End If

                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("DisplayPrev24Hours") Is Nothing Then
                            chkDisplayPrev24Hours.Checked = True
                        Else
                            chkDisplayPrev24Hours.Checked = CType(ModuleSettings("DisplayPrev24Hours"), Boolean)
                        End If

                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("DisplayOverallCount") Is Nothing Then
                            chkDisplayOverallCount.Checked = True
                        Else
                            chkDisplayOverallCount.Checked = CType(ModuleSettings("DisplayOverallCount"), Boolean)
                        End If

                        chkShowPeopleOnline.Checked = CType(ModuleSettings("ShowPeopleOnline"), Boolean)

                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("DisplayVisitorsOnline") Is Nothing Then
                            chkDisplayVisitors.Checked = True
                        Else
                            chkDisplayVisitors.Checked = CType(ModuleSettings("DisplayVisitorsOnline"), Boolean)
                        End If

                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("DisplayMembersOnline") Is Nothing Then
                            chkDisplayMembers.Checked = True
                        Else
                            chkDisplayMembers.Checked = CType(ModuleSettings("DisplayMembersOnline"), Boolean)
                        End If

                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("DisplayOnlineTotalCount") Is Nothing Then
                            chkDisplayOnlineTotals.Checked = True
                        Else
                            chkDisplayOnlineTotals.Checked = CType(ModuleSettings("DisplayOnlineTotalCount"), Boolean)
                        End If

                        chkShowUsersOnline.Checked = CType(ModuleSettings("ShowUsersOnline"), Boolean)

                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("NameDisplayMode") Is Nothing Then
                            ddlNameDisplayMode.Items.FindByValue("UserName").Selected = True
                        Else
                            ddlNameDisplayMode.Items.FindByValue(ModuleSettings("NameDisplayMode").ToString()).Selected = True
                        End If

                        'Show Membership exists, so this is an existing module. Preserve behavior!
                        If ModuleSettings("IncludeHosts") Is Nothing Then
                            chkIncludeHosts.Checked = True
                        Else
                            chkIncludeHosts.Checked = Boolean.Parse(ModuleSettings("IncludeHosts").ToString())
                        End If

                        If ModuleSettings("NamePrefix") Is Nothing Then
                            ddlNamePrefix.Items.FindByValue("Numbers").Selected = True 'Preserve Original behavior
                        Else
                            ddlNamePrefix.Items.FindByValue(ModuleSettings("NamePrefix").ToString()).Selected = True
                        End If

                        'If ModuleSettings("UserProfileMode") Is Nothing Then
                        '    rblstProfileMode.SelectedValue = "0" 'Off
                        'Else
                        '    rblstProfileMode.SelectedValue = ModuleSettings("UserProfileMode").ToString()
                        '    If rblstProfileMode.SelectedValue = "2" Then trUserProfileUrl.Visible = True
                        'End If

                        'If ModuleSettings("UserProfileUrl") Is Nothing Then
                        '    txtUserProfileUrl.Text = String.Empty
                        'Else
                        '    txtUserProfileUrl.Text = ModuleSettings("UserProfileUrl").ToString()
                        'End If
                    End If
                End If

                'Check for issues that may prevent UOL from running.
                'HostSetting Check
                Dim iSchedulerEnabled As Integer = Convert.ToInt32(Me.PortalSettings.HostSettings("SchedulerMode"))
                Dim bDisableUsersOnline As Boolean = Convert.ToBoolean(IIf(Me.PortalSettings.HostSettings("DisableUsersOnline").ToString() = "N", False, True))

                'Scheduler Check
                Dim bSchedulerEntityEnabled As Boolean = False
                Dim sKeySchedulerItem As String = "DotNetNuke.Entities.Users.PurgeUsersOnline, DOTNETNUKE"
                Dim alSchedulerItems As ArrayList = DotNetNuke.Services.Scheduling.SchedulingProvider.Instance().GetSchedule()
                If Not IsNothing(alSchedulerItems) Then
                    For Each oScheduledItem As DotNetNuke.Services.Scheduling.ScheduleItem In alSchedulerItems
                        If String.Equals("DotNetNuke.Entities.Users.PurgeUsersOnline, DOTNETNUKE", oScheduledItem.TypeFullName.Trim(), StringComparison.InvariantCultureIgnoreCase) Then
                            bSchedulerEntityEnabled = oScheduledItem.Enabled
                        End If
                    Next
                End If
                alSchedulerItems = Nothing

                'Build Status Messages.
                Dim lstMessages As New System.Text.StringBuilder()
                If iSchedulerEnabled = 0 Then lstMessages.Append(Localization.GetString("Status_SchedulerDisabled", Me.LocalResourceFile) & "<br />")
                If bDisableUsersOnline = True Then lstMessages.Append(Localization.GetString("Status_DisabledInHostSettings", Me.LocalResourceFile) & "<br />")
                If bSchedulerEntityEnabled = False Then lstMessages.Append(Localization.GetString("Status_DisabledInScheduler", Me.LocalResourceFile) & "<br />")
                Dim sStatusMessage As String = lstMessages.ToString()
                lstMessages = Nothing

                If String.IsNullOrEmpty(sStatusMessage) Then
                    imgStatus.ImageUrl = Me.ResolveUrl("~/Images/Green-Ok.gif")
                    imgStatus.AlternateText = Localization.GetString("Status_Ok", Me.LocalResourceFile)
                    lblStatus.Text = Localization.GetString("Status_Ok", Me.LocalResourceFile)
                Else
                    imgStatus.ImageUrl = Me.ResolveUrl("~/Images/Yellow-Warning.gif")
                    imgStatus.AlternateText = sStatusMessage.Replace("<br />", "")
                    lblStatus.Text = "<br />" & sStatusMessage
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' UpdateSettings saves the modified settings to the Database
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''		[cnurse]	10/25/2004	created
        '''  	[dbuckner]	06/05/2009	updated
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Overrides Sub UpdateSettings()
            Try
                Dim objModules As New Entities.Modules.ModuleController

                objModules.UpdateModuleSetting(ModuleId, "IncludeHosts", chkIncludeHosts.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "ShowMembership", chkShowMembership.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "DisplayLatestMember", chkDisplayLatestMember.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "DisplayPast24Hours", chkDisplayPast24Hours.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "DisplayPrev24Hours", chkDisplayPrev24Hours.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "DisplayOverallCount", chkDisplayOverallCount.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "ShowPeopleOnline", chkShowPeopleOnline.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "DisplayVisitorsOnline", chkDisplayVisitors.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "DisplayMembersOnline", chkDisplayMembers.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "DisplayOnlineTotalCount", chkDisplayOnlineTotals.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "ShowUsersOnline", chkShowUsersOnline.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "NameDisplayMode", ddlNameDisplayMode.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "NamePrefix", ddlNamePrefix.SelectedValue)
                'objModules.UpdateModuleSetting(ModuleId, "UserProfileMode", rblstProfileMode.SelectedValue)
                'objModules.UpdateModuleSetting(ModuleId, "UserProfileUrl", txtUserProfileUrl.Text)
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

#End Region

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        'Protected Sub rblstProfileMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblstProfileMode.SelectedIndexChanged

        '    If rblstProfileMode.SelectedValue = "2" Then
        '        trUserProfileUrl.Visible = True
        '    Else
        '        trUserProfileUrl.Visible = False
        '    End If

        'End Sub
    End Class

End Namespace

